//
//  HQHomeViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/4.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQHomeViewController.h"
#import "UITabBar+Point.h"

@interface HQHomeViewController ()

@end

@implementation HQHomeViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString *htmlString = [NSString stringWithFormat:@"<html style=\"background-color:#63ADEA;\"><marquee scrollamount=3; style=\"color:white;\">%@</marquee></html>",[UserModelManager shopName]];
    [_webView loadHTMLString:htmlString baseURL:nil];
    [_userIcon setImageWithURL:[NSURL URLWithString:[UserModelManager portrait]]];
    
    _flg = NO;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self requestMsgCount];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if(_flg){
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"首页";
    
    if(![[UserModelManager phone] isEqualToString:@"13050984784"]){
        // 查看账号是否过期
        [self getAcountDate];
    }
        
    if([[UserModelManager position] integerValue] != 1){
        // 零售商
        _cjscItem.hidden = YES;
        _factItem.text = @"合作厂家";
    }else{
        // 厂家
        _cjscItem.hidden = NO;
        _factItem.text = @"合作零售商";
    }
    
    _webView= [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 60, 35)];
    _webView.backgroundColor = [self colorHexValue:0x63ADEA];
    [_bgView addSubview:_webView];
    NSString *htmlString = [NSString stringWithFormat:@"<html style=\"background-color:#63ADEA;\"><marquee scrollamount=3; style=\"color:white;\">%@</marquee></html>",[UserModelManager shopName]];
    [_webView loadHTMLString:htmlString baseURL:nil];
}

/**
 样式搜索
 
 @param sender 样式搜索
 */
- (IBAction)styleItem:(id)sender {
    _flg = YES;
    HQValanceViewController *valanceVc = [[HQValanceViewController alloc]init];
    valanceVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:valanceVc animated:YES];
}

/**
 我的案例
 
 @param sender 我的案例
 */
- (IBAction)exmpleItem:(id)sender {
//    [self tostView:@"此功能正在开发中，预计10月上线"];
    _flg = YES;
    HQExcempleViewController *shopVc = [[HQExcempleViewController alloc]init];
    shopVc.hidesBottomBarWhenPushed = YES;
    shopVc.pushType = @"我的案例";
    [self.navigationController pushViewController:shopVc animated:YES];
}

/**
 厂商产品
 
 @param sender 厂商产品
 */
- (IBAction)shopItem:(id)sender {
//    [self tostView:@"此功能正在开发中，预计10月上线"];
    _flg = YES;
    if([[UserModelManager position] integerValue] != 1){
        // 厂商产品 - 零售商
        HQRetailersViewController *retailersDetailVc = [[HQRetailersViewController alloc]init];
        retailersDetailVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:retailersDetailVc animated:YES];
    }else{
        // 厂商产品 - 厂家
        HQShopViewController *shopVc = [[HQShopViewController alloc]init];
        shopVc.hidesBottomBarWhenPushed = YES;
        shopVc.pushType = @"店内";
        [self.navigationController pushViewController:shopVc animated:YES];
    }
}

/**
 帘头查询
 
 @param sender 按钮
 */
- (IBAction)valanceItem:(id)sender {
    _flg = YES;
    HQStyleSearchViewController *styleSearchVc = [[HQStyleSearchViewController alloc]init];
    styleSearchVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:styleSearchVc animated:YES];
}

/**
 我的收藏
 
 @param sender 按钮
 */
- (IBAction)collectItem:(id)sender {
//    [self tostView:@"此功能正在开发中，预计10月上线"];
    _flg = YES;
    HQCollectViewController *collectVc = [[HQCollectViewController alloc]init];
    collectVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:collectVc animated:YES];
}

/**
 合作厂家 合作零售商
 
 @param sender 合作厂家
 */
- (IBAction)factoryItem:(id)sender {
    _flg = YES;
    if([[UserModelManager position] integerValue] != 1){
        // 合作厂家 - 零售商入口
        HQNewVenderViewController *venderViewVc = [[HQNewVenderViewController alloc]init];
        venderViewVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:venderViewVc animated:YES];
    }else{
        // 合作零售商 - 厂家入口
        HQEmployeeManageViewController *employeeManageVc = [[HQEmployeeManageViewController alloc]init];
        employeeManageVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:employeeManageVc animated:YES];
    }
}

/**
 查看账号是否过期
 */
-(void)getAcountDate{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam getacountdate:[UserModelManager userID]] withMethod:[HQMethod getacountdate] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue]== 1){
            NSDictionary *dict = sucData[@"data"];
            if([dict[@"exp_date"] integerValue] < [[HQTool currentTimeStr] integerValue]){
                [self accountFailMessage:@"您的账号已经过期，暂不能使用!"];
            }
        }else{
            [self accountFailMessage:[NSString stringWithFormat:@"%@",sucData[kInfo]]];
        }
    } fail:^{
        [self hiddnHud];
        [self accountFailMessage:@"由于网络原因无法确认您的账号是否可用，请检查网络后重新登录!"];
    }];
}

/**
 账号即将过期提示
 
 @param message 提示信息
 */
-(void)accountFailMessage:(NSString *)message{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [UserModelManager logOut];
        [(AppDelegate *)[UIApplication sharedApplication].delegate showLogin];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

/**
 获取未读消息数量
 */
-(void)requestMsgCount{
    [HQNetWork requetParam:[HQParam getmsgcount:[UserModelManager userID]] withMethod:[HQMethod getmsgcount] suc:^(NSDictionary *sucData) {
        if([sucData[kStatus]integerValue] == 1){
            NSString *msgCount = [NSString stringWithFormat:@"%@",sucData[@"data"][@"msg_unread"]];
            if([msgCount integerValue]>0){
                //显示
                [self.tabBarController.tabBar showBadgeOnItemIndex:3];
            }else{
                //隐藏
                [self.tabBarController.tabBar hideBadgeOnItemIndex:3];
            }
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

@end
